<template>
    <el-dialog :visible.sync="isShow" :title="headerTitle" @close="handleClose" center top="14vh" width="364px" append-to-body :fullscreen="fullscreen">
        <el-form :model="dialogForm" size="small" label-position="top" label-suffix=":" ref="detailForm" :rules="rules" label-width="80px">
            <!--              大缺陷名称：-->
            <el-form-item :label="$t('basicData.MajorDefects')" prop="maxFlawName">
                <el-input v-model="dialogForm.maxFlawName" :placeholder="$t('public.inputPl')" clearable />
            </el-form-item>
            <!--              大缺陷名称：-->
            <el-form-item :label="$t('basicData.MajorDefectsCode')" prop="maxFlawCode">
                <el-input v-model="dialogForm.maxFlawCode" :placeholder="$t('public.inputPl')" clearable />
            </el-form-item>
            <!--              物料中类：-->
            <!-- <el-form-item :label="$t('basicData.materialClass')" prop="materialMiddle" ref="materialMiddle">
                <el-select v-model="dialogForm.materialMiddle" filterable size="small" style="width:100%" reserve-keyword :placeholder="$t('public.inputPl')+'物料中类'">
                    <el-option v-for="item in dict.type.NYIQC_MATERIAL_MIDDLE" :key="item.value" :label="item.label" :value="item.label">
                    </el-option>
                </el-select>
            </el-form-item> -->

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm" :loading="loading" icon="el-icon-document" size="small">{{$t('public.save')}}</el-button>
        </div>
    </el-dialog>
</template>

<script>
import {addMaxFlaw, updateMaxFlaw} from '@/api/basic/flawProject'
import EnjoySelectUser from '@/components/EnjoySelectUser'
import EnjoySelectDeptTree from '@/components/EnjoySelectDeptTree'
import FileUploadInput from '@/components/FileUploadInput'

export default {
    name: 'flaw-detial',
    components: { EnjoySelectUser, EnjoySelectDeptTree, FileUploadInput },
    dicts: [
        'NYIQC_MATERIAL_MIDDLE', //量具类型
    ],
    props: {
        value: {
            type: Object,
        },
        visible: {
            type: Boolean,
            default: false,
        },
        type: [String, Number], //1:新增  2：编辑 3：查看
    },
    watch: {
        value(val) {
            this.dialogForm = { ...val }
            this.materialRemoteMethod()
        },
    },
    computed: {
        isShow: {
            get() {
                return this.visible
            },
            set() {},
        },
        headerTitle() {
            const { type } = this
            let title = ''
            if (type == 1) {
                title =
                    this.$t('public.add') +
                    ' ' +
                    this.$t('defectIncoming.bigDefect')
            }
            if (type == 2) {
                title =
                    this.$t('public.edit') +
                    ' ' +
                    this.$t('defectIncoming.bigDefect')
            }

            return title
        },
    },
    data() {
        return {
            loading: false,
            fullscreen: false,
            deviceOptions: undefined,
            materialOptions: undefined,
            dialogForm: {
                maxFlawName: '',
                materialMiddle: '',
            },
            // 表单校验
            rules: {
                maxFlawName: [
                    {
                        required: true,
                        message: this.$t('formValidation.largeDefectNameCannotBeEmpty'),
                        trigger: 'blur',
                    },
                ],
                // maxFlawCode: [
                //     {
                //         required: true,
                //         message: '大缺陷编号不能为空',
                //         trigger: 'blur',
                //     },
                // ],
                materialMiddle: [
                    {
                        required: true,
                        message: this.$t('formValidation.theClassInTheMaterialCannotBeEmpty'),
                        trigger: 'blur',
                    },
                ],
            },
        }
    },
    methods: {
        /** 发起提交 */
        submitForm: function () {
            this.$refs.detailForm.validate((valid) => {
                let action = this.type == 1 ? addMaxFlaw : updateMaxFlaw
                if (valid) {
                    this.loading = true
                    action(this.dialogForm).then(
                        (res) => {
                            this.loading = false
                            if (res.code == 200) {
                                this.$emit('refresh')
                                this.$modal.msgSuccess('操作成功!')
                                this.$emit('update:visible', false)
                            }
                        },
                        (err) => {
                            this.loading = false
                        }
                    )
                }
            })
        },
        handleClose(done) {
            this.$emit('update:visible', false)
            this.$refs['detailForm'].resetFields()
        },

        materialRemoteMethod() {
            // listALLMaterialType().then((res) => {
            //     this.materialOptions = res.data
            // })
        },
        handleMaterialSelect(e) {
            this.dialogForm.materialMiddle = e.materialMiddle
            this.$refs.materialMiddle.$emit('el.form.blur')
        },
        handleMaterialBlur(e) {
            const { materialOptions, dialogForm } = this
            if (!materialOptions.includes(dialogForm.materialMiddle)) {
                // this.dialogForm.materialMiddle = ""
            }
        },
        handleExpireTime(e) {
            console.log(e)
            const today = new Date().getTime()
            const val = new Date(e).getTime()
            if (val > today) {
                this.dialogForm.remDay = ~~(
                    (val - today) /
                    1000 /
                    60 /
                    60 /
                    24
                ).toFixed(1)
            } else {
                this.dialogForm.remDay = 0
            }
            this.$forceUpdate()
        },
        handleRespName(e) {
            this.dialogForm.respName = e.name
        },
        handleEmployName(e) {
            this.dialogForm.employName = e.name
        },
        handleRespDept(e) {
            this.dialogForm.respDept = e.name
        },
    },
}
</script>

<style scoped>
</style>
